home *** CD-ROM | disk | FTP | other *** search
- /********************************************************************************
-
- Gnucleus - A node application for the Gnutella network
- Copyright (C) 2000 John Marshall
-
- This program is free software; you can redistribute it and/or modify
- it under the terms of the GNU General Public License as published by
- the Free Software Foundation; either version 2 of the License.
-
- This program is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- GNU General Public License for more details.
-
- You should have received a copy of the GNU General Public License
- along with this program; if not, write to the Free Software
- Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
-
- For support, questions, comments, etc...
- E-Mail:
- swabby@c0re.net
-
- Address:
- 21 Cadogan Way
- Nashua, NH, USA 03062
-
- ********************************************************************************/
-
- // Gnucleus.cpp : Defines the class behaviors for the application.
- //
-
- #include "stdafx.h"
- #include "Gnucleus.h"
-
- #include "MainFrm.h"
- #include "ChildFrm.h"
- #include "GnucleusDoc.h"
- #include "PrefDialog.h"
- #include "ViewConnect.h"
- #include "ViewNode.h"
- #include "ViewSearch.h"
- #include "ViewSearchSpy.h"
- #include "ViewTransfer.h"
- #include "ViewDebug.h"
- #include "Hyperlink.h"
- #include "AnimEffect.h"
-
- #include "GnuHash.h"
- #include "GnuControl.h"
-
- #include <stdlib.h>
- #include <time.h>
-
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
-
- /////////////////////////////////////////////////////////////////////////////
- // CGnucleusApp
-
- BEGIN_MESSAGE_MAP(CGnucleusApp, CWinApp)
- //{{AFX_MSG_MAP(CGnucleusApp)
- ON_COMMAND(ID_APP_ABOUT, OnAppAbout)
- ON_COMMAND(ID_CONNECT, OnConnect)
- ON_BN_CLICKED(IDC_BUTTON_SEARCH, OnButtonSearch)
- ON_COMMAND(ID_DEBUG, OnDebug)
- ON_COMMAND(ID_DLG_PREFS, OnDlgPrefs)
- ON_COMMAND(ID_TRANSFER, OnTransfer)
- ON_COMMAND(ID_VIEW_SEARCHSPY, OnViewSearchspy)
- ON_UPDATE_COMMAND_UI(ID_VIEW_SEARCHSPY, OnUpdateViewSearchSpy)
- //}}AFX_MSG_MAP
- // Standard file based document commands
- END_MESSAGE_MAP()
-
- /////////////////////////////////////////////////////////////////////////////
- // CGnucleusApp construction
-
- CGnucleusApp::CGnucleusApp()
- {
- MasterDoc = NULL;
-
- m_pConnectViewTemplate = NULL;
- m_pNodeViewTemplate = NULL;
- m_pSearchViewTemplate = NULL;
- m_pSearchSpyViewTemplate = NULL;
- m_pDebugViewTemplate = NULL;
- }
-
-
-
- /////////////////////////////////////////////////////////////////////////////
- // The one and only CGnucleusApp object
-
- CGnucleusApp theApp;
-
- /////////////////////////////////////////////////////////////////////////////
- // CGnucleusApp initialization
-
- BOOL CGnucleusApp::InitInstance()
- {
- // Seed random generator
- srand((unsigned)time(NULL));
-
- // Get saved hosts from cache
- // ((CGnucleusDoc *) MasterDoc)->ReadHostsFile();
-
-
-
- if (!AfxSocketInit())
- {
- AfxMessageBox(IDP_SOCKETS_INIT_FAILED);
- return FALSE;
- }
-
- MasterDoc = new CGnucleusDoc;
-
- // Standard initialization
- // If you are not using these features and wish to reduce the size
- // of your final executable, you should remove from the following
- // the specific initialization routines you do not need.
-
- #ifdef _AFXDLL
- Enable3dControls(); // Call this when using MFC in a shared DLL
- #else
- Enable3dControlsStatic(); // Call this when linking to MFC statically
- #endif
-
- // Change the registry key under which our settings are stored.
- // TODO: You should modify this string to be something appropriate
- // such as the name of your company or organization.
- SetRegistryKey(_T("Local AppWizard-Generated Applications"));
-
- LoadStdProfileSettings(); // Load standard INI file options (including MRU)
-
- // Load language DLL if set:
- CPrefLanguage::StaticSwitchLanguge(((CGnucleusDoc*)MasterDoc));
-
- // Register the application's document templates. Document templates
- // serve as the connection between documents, frame windows and views.
-
- m_pConnectViewTemplate = new CMultiDocTemplate(
- IDR_CONNECT,
- RUNTIME_CLASS(CGnucleusDoc),
- RUNTIME_CLASS(CChildFrame), // custom MDI child frame
- RUNTIME_CLASS(CViewConnect));
- AddDocTemplate(m_pConnectViewTemplate);
-
-
- m_pNodeViewTemplate = new CMultiDocTemplate(
- IDR_NODE,
- RUNTIME_CLASS(CGnucleusDoc), // document class
- RUNTIME_CLASS(CMDIChildWnd), // frame class
- RUNTIME_CLASS(CViewNode)); // view class
- AddDocTemplate(m_pNodeViewTemplate);
-
-
- m_pSearchViewTemplate = new CMultiDocTemplate(
- IDR_SEARCH,
- RUNTIME_CLASS(CGnucleusDoc), // document class
- RUNTIME_CLASS(CMDIChildWnd), // frame class
- RUNTIME_CLASS(CViewSearch)); // view class
- AddDocTemplate(m_pSearchViewTemplate);
-
- m_pSearchSpyViewTemplate = new CMultiDocTemplate(
- IDR_VIEW_SEARCH_SPY,
- RUNTIME_CLASS(CGnucleusDoc), // document class
- RUNTIME_CLASS(CMDIChildWnd), // frame class
- RUNTIME_CLASS(CViewSearchSpy)); // view class
- AddDocTemplate(m_pSearchSpyViewTemplate);
-
- m_pDebugViewTemplate = new CMultiDocTemplate(
- IDR_DEBUG,
- RUNTIME_CLASS(CGnucleusDoc), // document class
- RUNTIME_CLASS(CMDIChildWnd), // frame class
- RUNTIME_CLASS(CViewDebug)); // view class
- AddDocTemplate(m_pDebugViewTemplate);
-
-
- m_pTransfersViewTemplate = new CMultiDocTemplate(
- IDR_TRANSFER,
- RUNTIME_CLASS(CGnucleusDoc), // document class
- RUNTIME_CLASS(CChildFrame), // frame class
- RUNTIME_CLASS(CViewTransfer)); // view class
- AddDocTemplate(m_pTransfersViewTemplate);
-
-
- // create main MDI Frame window
- CMainFrame* pMainFrame = new CMainFrame;
- if (!pMainFrame->LoadFrame(IDR_MAINFRAME))
- return FALSE;
- m_pMainWnd = pMainFrame;
-
-
- // The main window has been initialized, so show and update it.
- pMainFrame->ShowWindow(SW_SHOWMAXIMIZED);
- pMainFrame->UpdateWindow();
-
- // Make Transfer window, initialize hidden
- TransferFrame = m_pTransfersViewTemplate->CreateNewFrame(MasterDoc, NULL);
- m_pTransfersViewTemplate->InitialUpdateFrame(TransferFrame, MasterDoc);
-
- TransferFrame->ModifyStyle(FWS_ADDTOTITLE, 0);
- TransferFrame->SetWindowText("Transfers");
- TransferFrame->ShowWindow(SW_HIDE);
-
- // Show Connect Frame
- ConnectFrame = m_pConnectViewTemplate->CreateNewFrame(MasterDoc, NULL);
- m_pConnectViewTemplate->InitialUpdateFrame(ConnectFrame, MasterDoc);
-
- // Cascade windows
- SendMessage(m_pMainWnd->m_hWnd, WM_COMMAND, ID_WINDOW_CASCADE, NULL);
-
- // Start listening for incoming connects
- ((CGnucleusDoc *) MasterDoc)->GnuComm->StartListening();
-
- return TRUE;
- }
-
-
- /////////////////////////////////////////////////////////////////////////////
- // CAboutDlg dialog used for App About
-
- class CAboutDlg : public CDialog
- {
- public:
- CAboutDlg();
-
- // Dialog Data
- //{{AFX_DATA(CAboutDlg)
- enum { IDD = IDD_ABOUTBOX };
- CHyperLink m_HyperlinkContact2;
- CHyperLink m_HyperlinkContact1;
- //}}AFX_DATA
-
- // ClassWizard generated virtual function overrides
- //{{AFX_VIRTUAL(CAboutDlg)
- protected:
- virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
- //}}AFX_VIRTUAL
-
- // Implementation
- protected:
- //{{AFX_MSG(CAboutDlg)
- afx_msg void OnShowWindow(BOOL bShow, UINT nStatus);
- afx_msg void OnDestroy();
- //}}AFX_MSG
- DECLARE_MESSAGE_MAP()
- };
-
- CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
- {
- //{{AFX_DATA_INIT(CAboutDlg)
- //}}AFX_DATA_INIT
- }
-
- void CAboutDlg::DoDataExchange(CDataExchange* pDX)
- {
- CDialog::DoDataExchange(pDX);
- //{{AFX_DATA_MAP(CAboutDlg)
- DDX_Control(pDX, IDC_STATIC_CONTACT2, m_HyperlinkContact2);
- DDX_Control(pDX, IDC_STATIC_CONTACT1, m_HyperlinkContact1);
- //}}AFX_DATA_MAP
- }
-
- BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
- //{{AFX_MSG_MAP(CAboutDlg)
- ON_WM_SHOWWINDOW()
- ON_WM_DESTROY()
- //}}AFX_MSG_MAP
- END_MESSAGE_MAP()
-
- void CAboutDlg::OnShowWindow(BOOL bShow, UINT nStatus)
- {
- if( bShow )
- {
- ShowAnimEffect(this, TRUE);
- }
-
- CDialog::OnShowWindow(bShow, nStatus);
- }
-
- void CAboutDlg::OnDestroy()
- {
- CDialog::OnDestroy();
-
- ShowAnimEffect(this, FALSE);
- }
-
- // App command to run the dialog
- void CGnucleusApp::OnAppAbout()
- {
- CAboutDlg aboutDlg;
- aboutDlg.DoModal();
- }
-
- /////////////////////////////////////////////////////////////////////////////
- // CGnucleusApp message handlers
-
- void CGnucleusApp::OnConnect()
- {
- bool Found = 0;
- POSITION pos = MasterDoc->GetFirstViewPosition();
-
- while (pos != NULL)
- {
- CView* pView = MasterDoc->GetNextView(pos);
-
- if(pView->GetParentFrame() == ConnectFrame)
- {
- ConnectFrame->ShowWindow(SW_SHOWNORMAL);
- ConnectFrame->BringWindowToTop();
-
- return;
- }
- }
-
- ConnectFrame = m_pConnectViewTemplate->CreateNewFrame(MasterDoc, NULL);
-
- m_pConnectViewTemplate->InitialUpdateFrame(ConnectFrame, MasterDoc);
- }
-
- void CGnucleusApp::OnButtonSearch()
- {
- CString TestCase, Keyword, Speed;
-
- CComboBox *cbKeyword = (CComboBox *) ( (CMainFrame *) m_pMainWnd )->GetDialogBar()->GetDlgItem(IDC_COMBO_SEARCH);
- cbKeyword->GetWindowText(Keyword);
-
- CString Search = "Searching for \"";
- Search += Keyword;
- Search += "\"";
-
- // Make sure search isnt null
- if(Keyword == "")
- return;
-
- // Make sure user isnt flooding searches
- if(cbKeyword->GetCount() != 0)
- cbKeyword->GetLBText(0, TestCase);
-
- if(TestCase != Keyword)
- cbKeyword->InsertString(0, Keyword);
-
- // Bring window to front if it already exists
- bool Found = 0;
- CString Title;
- POSITION pos = MasterDoc->GetFirstViewPosition();
-
- while (pos != NULL)
- {
- CView* pView = MasterDoc->GetNextView(pos);
- pView->GetParentFrame()->GetWindowText(Title);
-
- if(Title == Search)
- {
- pView->GetParentFrame()->BringWindowToTop();
-
- return;
- }
- }
-
- // Create new search window
- CFrameWnd* pNewFrame = m_pSearchViewTemplate->CreateNewFrame(MasterDoc, NULL);
-
- pNewFrame->ModifyStyle(FWS_ADDTOTITLE, 0);
- pNewFrame->SetWindowText(Search);
-
- m_pSearchViewTemplate->InitialUpdateFrame(pNewFrame, MasterDoc);
- }
-
-
- void CGnucleusApp::OnDebug()
- {
- //CFrameWnd* pNewFrame = m_pDebugViewTemplate->CreateNewFrame(MasterDoc, NULL);
- //m_pDebugViewTemplate->InitialUpdateFrame(pNewFrame, MasterDoc);
- }
-
- void CGnucleusApp::OnDlgPrefs()
- {
- CPrefDialog PrefDlg( _T("Preferences"));
- PrefDlg.DoModal();
- }
-
-
- void CGnucleusApp::OnTransfer()
- {
- TransferFrame->ShowWindow(SW_SHOW);
- TransferFrame->BringWindowToTop();
- }
-
-
- ////////////////////////////////////////////////////////////
- //! author="Nathan Brown"
- //
- //: Display the animation effect for open / close dialog
- void ShowAnimEffect( CWnd* pWnd, BOOL bOpen )
- {
- CRect oRect;
- AnimEffect oAnimEffect;
-
- if( pWnd )
- {
- pWnd->GetWindowRect( oRect );
- bOpen ? oAnimEffect.Open(oRect) : oAnimEffect.Close(oRect);
- }
- }
-
-
-
- int CGnucleusApp::ExitInstance()
- {
- // In case you load multiple DLL's make sure to free them,
- // and avoid calling FreeLibrary with a NULL pointer.
-
- if(((CGnucleusDoc*)MasterDoc)->m_hLangDLL)
- FreeLibrary(((CGnucleusDoc*)MasterDoc)->m_hLangDLL);
-
- return CWinApp::ExitInstance();
- }
-
- //
- // If there is a search spy window then close it,
- // otherwise create one.
- //
- void CGnucleusApp::OnViewSearchspy()
- {
- POSITION pos = MasterDoc->GetFirstViewPosition();
- CView* pView = 0;
- CString Title;
-
- while (pos != NULL)
- {
- pView = MasterDoc->GetNextView(pos);
- pView->GetParentFrame()->GetWindowText(Title);
-
- if(Title == "Search Spy" )
- {
- //pView->GetParentFrame()->CloseWindow();
- pView->GetParentFrame()->DestroyWindow();
- return;
- }
- }
-
- // Not found .. so create one.
- CFrameWnd* pNewFrame = m_pSearchSpyViewTemplate->CreateNewFrame(MasterDoc, NULL);
- pNewFrame->ModifyStyle(FWS_ADDTOTITLE, 0);
- pNewFrame->SetWindowText("Search Spy");
- m_pSearchSpyViewTemplate->InitialUpdateFrame(pNewFrame, MasterDoc);
- }
-
- //
- // See if the search spy window is visible, and
- // set the check on the menu appropriately.
- //
- void CGnucleusApp::OnUpdateViewSearchSpy(CCmdUI *pui)
- {
- bool Found = 0;
- CString Title;
- POSITION pos = MasterDoc->GetFirstViewPosition();
-
- while (pos != NULL)
- {
- CView* pView = MasterDoc->GetNextView(pos);
- pView->GetParentFrame()->GetWindowText(Title);
-
- if(Title == "Search Spy" )
- {
- Found = true;
- break;
- }
- }
-
- pui->SetCheck( Found );
- }
-